#include "av.h"

extern
int lua_output_format_guess(lua_State * L) {
	const char * short_name = lua_tostring(L, 1);
	const char * filename = lua_tostring(L, 2);
	const char * mime_type = lua_tostring(L, 3);
	AVOutputFormat *fmt = guess_format(short_name, filename, mime_type);
	if (fmt) {
		lua_push_output_format(L, fmt);
		return 1;
	}
	return 0;
}

lua_constant output_flag_consts[] = {
	{ "nofile", AVFMT_NOFILE },
	{ "neednumber", AVFMT_NEEDNUMBER },
	{ "show_ids", AVFMT_SHOW_IDS },
	{ "rawpicture", AVFMT_RAWPICTURE },
	{ "globalheader", AVFMT_GLOBALHEADER },
	{ "notimestamps", AVFMT_NOTIMESTAMPS },
	{ "generic_index", AVFMT_GENERIC_INDEX },
	{ "ts_discont", AVFMT_TS_DISCONT },
	{ "variable_fps", AVFMT_VARIABLE_FPS },
	{ NULL, 0 },
};

static
void lua_push_output_flag(lua_State * L, int flags) {
	lua_push_flag(L, flags, output_flag_consts);
}

DEFINE_TYPE_STRUCT(output_format, AVOutputFormat,
	LUA_OBJECT_OUTPUT_FORMAT_ID, 0, 0)

DEFINE_INDEX_HEADER(output_format, AVOutputFormat)
	HANDLE_INDEX_ENTRY(name, lua_pushstring);
	HANDLE_INDEX_ENTRY(long_name, lua_pushstring);
	HANDLE_INDEX_ENTRY(mime_type, lua_pushstring);
	HANDLE_INDEX_ENTRY(extensions, lua_pushstring);
	HANDLE_INDEX_ENTRY(priv_data_size, lua_pushinteger);
	HANDLE_INDEX_ENTRY(audio_codec, lua_push_codec_id);
	HANDLE_INDEX_ENTRY(video_codec, lua_push_codec_id);
	HANDLE_INDEX_ENTRY(flags, lua_push_output_flag);
DEFINE_INDEX_TRAILER()

DEFINE_NEWINDEX_HEADER(output_format, AVOutputFormat)
DEFINE_NEWINDEX_TRAILER()

DEFINE_OBJECT_FUNCTIONS(output_format, AVOutputFormat)
